package cn.lili.controller.operationcenter;

import cn.lili.cache.auth.annotation.Authentication;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.operationcenter.entity.dos.OperationCenterWithdrawal;
import cn.lili.modules.operationcenter.entity.dto.OperationCenterWithdrawalDTO;
import cn.lili.modules.service.OperationCenterWithdrawalService;
import cn.lili.modules.system.client.SmsClient;
import cn.lili.modules.system.entity.params.SmsParams;
import cn.lili.modules.verification.entity.enums.VerificationEnums;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;



@RestController
@Api(tags = "运营中心提现控制器")
@RequestMapping("/operationCenter")
@RequiredArgsConstructor
public class OperationCenterWithdrawalController {

    private final OperationCenterWithdrawalService operationCenterWithdrawalService;

    private final SmsClient smsClient;
    @GetMapping("/selectOperationCenterWithdrawalPageList")
    @ApiOperation("运营中心-分页获取运中心-绿色积分提现记录")
    public ResultMessage<Page<OperationCenterWithdrawal>> selectOperationCenterWithdrawalPageList(OperationCenterWithdrawalDTO dto) {
        AuthUser authUser = UserContext.getCurrentUser();
        dto.setUserId(authUser.getId());
        return ResultUtil.data(operationCenterWithdrawalService.selectOperationCenterWithdrawalPageList(dto));
    }

    @GetMapping("/selectOperationCenterWithdrawalPageAllList")
    @ApiOperation("管理端-分页获取运中心-绿色积分提现记录")
    public ResultMessage<Page<OperationCenterWithdrawal>> selectOperationCenterWithdrawalPageAllList(OperationCenterWithdrawalDTO dto) {
        return ResultUtil.data(operationCenterWithdrawalService.selectOperationCenterWithdrawalPageList(dto));
    }


    @PutMapping("/greenWithdrawal/{money}")
    @ApiOperation("运营中心-绿色积分申请提现")
    @PreventDuplicateSubmissions
    public ResultMessage<Boolean> greenWithdrawal(@PathVariable String money) {
        return ResultUtil.data(operationCenterWithdrawalService.greenWithdrawal(money));
    }

    @PostMapping("/applyWithdrawal")
    @ApiOperation("运营中心-绿色积分提现审核")
    @PreventDuplicateSubmissions
    public ResultMessage<Boolean> applyWithdrawal(@RequestBody OperationCenterWithdrawal dto) {
        return ResultUtil.data(operationCenterWithdrawalService.applyWithdrawal(dto));
    }

    @ApiOperation("校验短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobilePhone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "scene", value = "场景", required = true, paramType = "header"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query")})
    @PostMapping("/checkSmsCode")
    public ResultMessage<Boolean> checkSmsCode(@NotNull(message = "mobile cannot be empty") @RequestParam String mobilePhone,
                                               @NotNull(message = "scene cannot be empty") @RequestHeader String scene,
                                               @NotNull(message = "code cannot be empty") @RequestParam String code,
                                               @RequestHeader String uuid) {
        Boolean flag = smsClient.verifyCode(new SmsParams(mobilePhone, VerificationEnums.OPERATION_SET_BANK_ACCOUNT, uuid, code,
                SceneEnums.getScene(scene)));
        if (flag != null && flag) {
            return ResultUtil.data(true);
        } else {
            return ResultUtil.error(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
        }
    }
}